package cn.wolfcode.luowowo.website.web;

import cn.wolfcode.luowowo.website.annotion.RequireLogin;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
public class UserInfoController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/get")
    @ResponseBody
    @RequireLogin
    public Object get(Long id){
        return userInfoService.get(id);
    }

    //检查手机号是否唯一
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    //短信验证码的发送
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    //注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,
                             String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }

    //登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password,
                            HttpServletRequest request, HttpServletResponse response){
      String token = userInfoService.userLogin(username,password);
        /*  ,//cookie和response都是表现层的对象, 不建议在业务层使用
        Cookie c = new Cookie("token",token);
        c.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
        c.setPath("/");//默认共享路径是/xxx,如果遇到/xx/yy就不能共享了,所以要改
        response.addCookie(c);*/
        int time = RedisKeys.USER_LOGIN_TOKEN.getTime().intValue();
        CookieUtil.addCookie("token",token,time,response);
        return AjaxResult.SUCCESS;
    }


}
